
// ##############################
// ####  note:  bokeweilai common main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#ifndef __BKWL_COMMON_MAIN_H__
#define __BKWL_COMMON_MAIN_H__
#include "bk_private/bk_init.h"
#include <components/system.h>
#include <os/os.h>
#include <components/shell_task.h>
#include "cli.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "pthread.h"
#include <components/event.h>
#include <components/netif.h>

#include "time/time.h"
#include <common/bk_include.h>
#include "bk_arm_arch.h"
#include <driver/aon_rtc_types.h>
#include <string.h>
#include <driver/aon_rtc.h>

#include "dev_log.h"
#include "bkwl_bk_doorbell.h"

#define LOG_BUF_SIZE 254 * 3
#define LOG_BUF_LEN 3

#define BK_RUN_MODE_NORMAL 0
#define BK_RUN_MODE_CALL 1
#define BK_RUN_MODE_NOW BK_RUN_MODE_NORMAL

#define BKWL_LOG_NULL 0
#define BKWL_LOG_DEBUG 1
#define BKWL_LOG_USER 2
#define BKWL_LOG_CUR BKWL_LOG_USER

#define BKWL_IOT_VER_100 "1.0.0"
#define BKWL_IOT_VER_CUR BKWL_IOT_VER_100

#define BKWL_PRODUCT_USER_NAME "C_0DIrDBf1j0SFbUGI"
#define BKWL_PRODUCT_BK7258_USERNAME "P_1jOMUMWtsEeP8PLP"
#define BKWL_PRODUCT_BK7258_SECRET "HeNA2JsMriYbuGGbHWSgSHBbGPYpp6dT"

#define BKWL_HTTP_POST_UPLOAD_IMAGE "http://m.yo-jia.cn/api/wx/upload/upload.php"
#define BKWL_HTTP_GET_TIME "http://m.yo-jia.cn/api/wx/mqtt/getTime.php"
#define BKWL_HTTP_GET_MQTT "http://m.yo-jia.cn/api/wx/mqtt/getMqtt.php"
#define BKWL_HTTP_GET_DEVICE "http://m.yo-jia.cn/api/wx/app/api?c=license&a=getLicenseInfo&uname="

#define BKWL_IOT_DOORBELL_UART_ID 2

//
typedef enum
{
    BKWL_WIFI_STATION_CONNECT,
    BKWL_WIFI_STATION_CONNECTED,
    BKWL_WIFI_DATA_CONNECT,
    BKWL_WIFI_STATION_DISCONNECTED,

    BKWL_START_WIFI_STATION,
    BKWL_START_TCP_SERVICE,
    BKWL_START_BOARDING_EVENT,
    BKWL_BLE_DISABLE,
    BKWL_SDP,
    BKWL_EXIT,
    BKWL_IMAGE_TCP_SERVICE_DISCONNECTED,

    BKWL_CMD_MQTT_SEND_DATA,

    BKWL_CMD_BOARD_INIT,
    BKWL_CMD_CAMERA_INIT,
    BKWL_CMD_HTTP_POST_IMAGE,
    BKWL_WIFI_STATION_BKWL_CONNECT,
    BKWL_CMD_INIT_MQTT,
    BKWL_CMD_LCD_DISPLAY,
    BKWL_CMD_AGORA_INIT,

} bkwl_event_cmt_e;

typedef struct
{
    uint32_t event;
    uint32_t param;
} bkwl_msg_t;

typedef struct
{
    beken_thread_t msg;
} bkwl_queue_t;

typedef struct
{
    beken_thread_t msg;
    beken_thread_t event;
} bkwl_task_t;

typedef struct
{
    uint32_t len;
    uint8_t *buffer;
} bkwl_image_t;

typedef struct
{
    bkwl_image_t image_cur;
    uint8_t image_print;
    uint8_t image_get;
} bkwl_camera_t;

typedef struct
{
    char MQTT_PROPERTY_EVENT_POST[100];
    char MQTT_PROPERTY_SERVICE_SET[100];
    char MQTT_NOTIFY_EVENT_POST[100];
    char MQTT_NOTIFY_EVENT_POST_REPLY[100];
    char MQTT_SERVICE_GET[100];
    char MQTT_SERVICE_GET_REPLY[100];
} bkwl_topic_sibo_t;

typedef struct
{
    char ssid[32];
    char password[32];
    char token[64];
    char cl_token[64];
} bkwl_bt_pair_t;

typedef struct
{
    char host[36];
    uint32_t port;
    char device_name[36];
    char device_secret[36];
    char product_key[36];

} bkwl_mqtt_info_t;

typedef struct
{
    uint8_t mqtt_connect;

} bkwl_status_t;

typedef struct
{
    bkwl_topic_sibo_t topic;
    bkwl_queue_t queue;
    bkwl_task_t task;
    bkwl_camera_t camera;
    bkwl_bt_pair_t bt;
    bkwl_mqtt_info_t mqtt;
    bkwl_status_t status;
} bkwl_main_t;

extern bkwl_main_t *bkwl_main;
extern bk_err_t bkwl_common_main();

#endif
